'''UDP协议实现：客户端发送命令’服务端接收命令并把命令执行后的结果发送给客户端'''
import subprocess
from socket import *
ip_port = ("localhost", 51590)
buffer_size = 1024
back_log=5
udp_server=socket(AF_INET,SOCK_DGRAM)
udp_server.bind(ip_port)
while True:
    print("wait for send message...")
    cmd,addr = udp_server.recvfrom(buffer_size)
    print("收到客户端发送的消息是：",cmd)
    res = subprocess.Popen(cmd.decode("utf-8"), shell=True,
                           stderr=subprocess.PIPE,
                           stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE)
    err = res.stderr.read()
    if err:
        cmd_res = err
    else:
        cmd_res = res.stdout.read()
    if not cmd_res:
        cmd_res = "执行成功".encode("gbk")
    udp_server.sendto(cmd_res,addr)
udp_server.close()